require(["config"],function(){
	require(["jquery","template","header","cookie"],function($,template){
		
		function Cart(){
			this.products = null;
			this.load();
			this.addListener();
			
		}
		
		Cart.prototype = {
			constructor:Cart,
			load:function(){
				$.cookie.json = true;
				var products = this.products = $.cookie("products")||[];
				if(products.length === 0){
					$(".empty").show().siblings(".not-empty").hide();
					return;
				}
				//购物车非空
				else{
					$(".empty").hide().siblings(".not-empty").removeClass("hidden");
					var data = {products:products};
					var html = template("cart-body-template",data);
					$(".cart-table tbody").html(html);
				}
			},
			//注册加减的事件监听
			addListener: function(){
				//删除
				$(".cart-table tbody").on("click",".del",$.proxy(this.delHander,this));
				//增加
				$(".cart-table tbody").on("click",".decrement,.increment",$.proxy(this.delIncHander,this));
				//输入修改数量
				$(".cart-table tbody").on("keyup",".amount",$.proxy(this.inputHander,this));
				//全选
				$(".ck_all").on("click",$.proxy(this.ckAllHander,this));
				//部分选中
				$(".cart-table tbody").on("click", ".ck_prod", $.proxy(this.ckProdHandler, this));
			},
			//删除
			delHander: function(event){
				var src = event.target;
				var tr = $(src).parents("tr");
//				console.log($(src));
				var id = tr.find(".id").text();
//				console.log(this.products);
				this.products = this.products.filter(function(prod){
					if(prod.id === id)
						return false;
					return true;
				});
				$.cookie("products", this.products, {expires: 10, path: "/"});
				tr.remove();
				this.calcTotal();
			},
			//加减
			delIncHander: function(event){
				var src = event.target;
				var tr = $(src).parents("tr");
				var id = tr.find(".id").text();
				var product = this.products.filter(function(prod){
					return prod.id === id;
				})[0];
				if($(src).is(".decrement")){
					if(product.amount <= 1)
						return;
					product.amount--;
				}else{
					product.amount++;
				}
				//保存到cookie中(修改数量后的数组)
				$.cookie("products",this.products,{expires:10,path:"/"});
				tr.find(".amount").val(product.amount);
				tr.find(".sub").text(product.amount*product.price);
				this.calcTotal();
			},
			
			//输入改商品数量
			inputHander:function(event){
				var src = event.target;
				var tr = $(src).parents("tr");
				var id = tr.find(".id").text();
				var product = this.products.filter(function(prod){
					return prod.id === id;
				})[0];
				var _amount = $(src).val();
				var reg = /^[1-9]\d*$/;
				if(!reg.test(_amount)){
					$(src).val(product.amount);
					return;
				}
				product.amount = _amount;
				tr.find(".sub").text(product.amount*product.price);
				$.cookie("products",this.products,{expires:10,path:"/"})
				this.calcTotal();
			},
			ckAllHander: function(event){
				var status = $(event.target).prop("checked");
//				console.log(status);
				$(".ck_prod").prop("checked",status);
				console.log(this);
				this.calcTotal();
			},
			ckProdHandler: function(event){
				var count = $(".ck_prod:checked").length;
				var status = count === this.products.length;
				$(".ck_all").prop("checked",status);
				this.calcTotal();
			},
			calcTotal:function(){
				var sum = 0;
				$(".ck_prod:checked").each(function(index,element){
					sum += Number($(this).parents("tr").find(".sub").text());
				});
				$("tfoot .total").text(sum);
			}
		}
		new Cart();
	});
});